package Objects;

import org.newdawn.slick.geom.Polygon;
import org.newdawn.slick.geom.Transform;

import LBC.CollisionListener;

public class BodyRectangle extends Body {

	public int width;
	public int height;
	public Polygon polygon;
	
	public BodyRectangle(int mass, float x1, float y1, int width, int height, float velocityX, float velocityY , float accelerationX, float accelerationY, float angle, float angularVelocity, float forceX, float forceY, float epsilon) {
		super(mass, velocityX, velocityY, angle, angularVelocity, forceX, forceY, epsilon);
		this.polygon=new Polygon();
		polygon.addPoint(x1, y1);
		polygon.addPoint(x1, y1+height);
		polygon.addPoint(x1+width, y1+height);
		polygon.addPoint(x1+width, y1);
		super.shape=this.polygon;
		this.momentOfInertia=(float)((mass*(Math.pow(width,2)+Math.pow(height,2)))/12);
		this.CMX=width/2;
		this.CMY=height/2;
		this.COR=1;
	}
	
	public BodyRectangle(int mass, float x1, float y1, int width, int height){
		super(mass);
		this.polygon=new Polygon();
		polygon.addPoint(x1, y1);
		polygon.addPoint(x1, y1+height);
		polygon.addPoint(x1+width, y1+height);
		polygon.addPoint(x1+width, y1);
		super.shape=this.polygon;
		this.momentOfInertia=(float)((mass*(Math.pow(width,2)+Math.pow(height,2)))/12);
		this.CMX=width/2;
		this.CMY=height/2;
		this.COR=1;
	}
	
	public void addColisionListener(CollisionListener listener){
		//listener.addBodyR(this);
	}

	@SuppressWarnings("static-access")
	public void rotate(float angle){
		Transform rotation=new Transform();
		this.polygon=(Polygon) this.polygon.transform(rotation.createRotateTransform(angle, this.polygon.getCenterX(), this.polygon.getCenterY()));
	}
	

	
	public float[] findNormal(float pX, float pY){
		System.out.println("GRRRR");
		int X=0;
		int Y=1;
		float result=2000000000;
		float p1[]={0f, 0f};
		float p2[]={0f, 0f};
		float temp;
		float edge[]={0f, 0f};
		
		for(int i=0; i<4; i++)
		{
			System.out.println(i);
			p1=this.polygon.getPoint(i);
			if(i!=3){p2=this.polygon.getPoint(i+1);}
			else {p2=this.polygon.getPoint(1);}
			temp=check(p1,p2, pX, pY);
			if (temp<result) 
			{
			result=temp;
			edge[X]=p2[X]-p1[X];
			edge[Y]=p2[Y]-p1[Y];
			}
		}
		//float edgeV=(float)(Math.sqrt(Math.pow(edge[X],2)+Math.pow(edge[Y],2)));
		float normal[]=new float[2];
		normal[X]=edge[Y];
		normal[Y]=-edge[X];
		float normalV=(float)(Math.sqrt(Math.pow(normal[X],2)+Math.pow(normal[Y],2)));
		normal[X]=normal[X]/Math.abs(normalV);
		normal[Y]=normal[Y]/Math.abs(normalV);
		
		return normal;
	}
	
	public float check(float[] p1, float[] p2, float pX, float pY){
		int X=0;
		int Y=1;
		float result=0f;
		if(p1[X]==p2[X]) result=Math.abs(p1[X]-pX);
		else
		{
			result=Math.abs((p2[Y]-p1[Y])/(p2[X]-p1[X])*(pX-p1[X])-pY+p1[Y]);
		}	
		return result;
	}

	public float[] getSupportingPoint(float[] u) {
		int max=0;
		for ( int i=0; i<4 ; i++){
			float [] A=this.shape.getPoint(i);
			float [] B=this.shape.getPoint(max);
			if((u[0]*(A[0]-B[0])+u[1]*(A[1]-B[1]))>0)
				max=i;
		}
		return this.shape.getPoint(max);
	}

}
